﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CThru.Silverlight;
using System.Windows;
using TypeMock.ArrangeActAssert;

namespace ThemeLoader.Test.Unit
{
    [TestFixture]
    public class DelegateResourceProviderTests
    {
        private DelegateResourceProvider resourceProvider;
        private ResourceDictionary resources;
        private bool wasCalled = false;

        [SetUp, SilverlightSetUp]
        public void Setup()
        {
            this.wasCalled = false;
            this.resources = Isolate.Fake.Instance<ResourceDictionary>();
            Isolate.WhenCalled(() => this.resources["key"]).DoInstead(context => new object());
            this.resourceProvider = new DelegateResourceProvider(() => 
                {
                    wasCalled = true;
                    return resources;
                });
        }

        [Test, SilverlightUnitTest]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Constructor_NullDelegate_ThrowArgumentNullException()
        {
            var provider = new DelegateResourceProvider(null);
        }

        [Test, SilverlightUnitTest]
        public void GetResourceDictionary_Always_WasCalledIsTrue()
        {
            resourceProvider.GetResourceDictionary();

            Assert.IsTrue(wasCalled);
        }

        [Test, SilverlightUnitTest]
        public void GetResourceDictionary_Always_DictionaryIsReturned()
        {
            var dictonary = resourceProvider.GetResourceDictionary();

            Assert.AreEqual(this.resources, dictonary);
        }

        [Test, SilverlightUnitTest]
        public void GetResource_Always_WasCalledIsTrue()
        {
            resourceProvider.GetResource("key");

            Assert.IsTrue(wasCalled);
        }

        [Test, SilverlightUnitTest]
        public void GetResource_Always_ResourceIsFetchedByKey()
        {
            resourceProvider.GetResource("key");

            Isolate.Verify.WasCalledWithExactArguments(() => this.resources["key"]);
        }
    }
}
